<?php

namespace App\Domain\Movie;
use App\Model\Movie\MovieList as MovieListModel;
use App\Model\Movie\MovieCategory as MovieCategoryModel;
use App\Model\Movie\Domain as DomainModel;

/**
 * 用户
 *
 * - 可用于自动生成一个新用户
 *
 * @author dogstar 20200331
 */

class MovieList {

    public function getList($limit,$page) {

        $model = new MovieListModel();

        return $model->getPerList($limit,$page);
    }
    public function getHome() {

        $model = new MovieListModel();
        $cate = new MovieCategoryModel();
        $cates = $cate->getInfoByPid(2);
        $sourseids = [];
        foreach ($cates as $key=>$value){
            $sourseids[]=$value['source_id'];
        }
        $res = $model->getHome($sourseids);
        return $res;
    }
    public function getMovieInfo($id) {
        $domain = new MovieListModel();
        $res = $domain->getMovieInfo($id);
        $cate = new MovieCategoryModel();
        $cate = $cate->getCateInfoBySourceId($res['cate_id']);
        $res['tv']=$cate;
        $res['status']='已完结';
        $res['tags'] = explode('，',$res['tag'])[0] != ''?explode('，',$res['tag']):['暂无'];
        $res['hits'] = rand(1,9000);
        return $res;
    }
    public function getMovieByPid($pid,$limit=10,$page=0) {
        $list = new MovieListModel();
        $cate = new MovieCategoryModel();
        $cates = $cate->getInfoByPid($pid);
        $category = [];
        $sourseids = [];
        foreach ($cates as $key=>$value){
            $category[$key]=['txt'=>$value['name'],'source_id'=>$value['source_id']];
            $sourseids[]=$value['source_id'];
        }
        $lists = $list->getMovieByPid($sourseids,$limit,$page);
        $years = $list->getMovieYearsByPid($sourseids);
        return ['list'=>$lists,'year'=>$years,'category'=>$category];
    }

    public function getMovieByYear($pid,$year,$limit=10,$page=0) {
        $list = new MovieListModel();
        $cate = new MovieCategoryModel();
        $cates = $cate->getInfoByPid($pid);
        $sourseids = [];
        foreach ($cates as $key=>$value){
            $sourseids[]=$value['source_id'];
        }
        $res = $list->getMovieByYear($sourseids,$year,$limit,$page);
        return ['list'=>$res];
    }
    public function getMovieBySourceId($cid,$limit=10,$page=0) {
        $list = new MovieListModel();
        $res = $list->getMovieByPid($cid,$limit,$page);
        return ['list'=>$res];
    }

    public function searchMovieList($title,$limit=10,$page=0) {
        $domain = new MovieListModel();
        $res = $domain->searchMovieList($title,$limit,$page);
        return $res;
    }
    public function domainMovieList($domain,$limit=10,$page=0) {
        $model = new MovieListModel();
        $res = $model->domainMovieList($domain,$limit,$page);
        return $res;
    }

    public function getLikeList() {
        $domain = new MovieListModel();
        $res = $domain->getLikeList();
        return $res;
    }

    public function searchList($title,$limit,$page) {

        $model = new MovieListModel();

        return $model->searchList($title,$limit,$page);
    }
    public function update($id, $newData) {
        $model = new MovieListModel();
        if($id != 0) {
            return $model->update($id, $newData);
        }else{
            return $model->insert($newData);
        }
    }
    public function delete($id){
        $model = new MovieListModel();
        return $model->delete($id);
    }
    public function setDomains(){
        $model = new MovieListModel();
        $domain = new DomainModel();
        $domains = $domain->getList();
        $res = $model->setDomains($domains);
        if($res) {
            return 1;
        }
    }
    public function removeDomains(){
        $model = new MovieListModel();
        $model->updateAll(['id>=?',0],['domain'=>'']);
    }
}